﻿using Furion.CMS.Application.Dto;
using Furion.CMS.Core;
using Furion.CMS.Domain;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    /// <summary>
    /// 网址配置
    /// </summary>
    [DisplayName("WebSite")]
    public class SiteSettingAppService : BaseApplication, IScoped
    {
        private readonly IRepository<SiteSetting> _siteRepository;

        public SiteSettingAppService(IRepository<SiteSetting> siteRepository)
        {
            _siteRepository = siteRepository;
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        [SecurityDefine]
        [ApiDescriptionSettings(Name = "base")]
        public SiteSettingsDto GetSettings()
        {
            // 从当前身份凭据中获取数据
            var tenantId = App.User?.FindFirstValue(AppClaimConsts.TenantId);
            var hasTenant = Guid.TryParse(tenantId, out Guid tid);
            return _siteRepository.AsQueryable(false).Where(hasTenant, x => x.TenantId == tid)
                .FirstOrDefault()
                .Adapt<SiteSettingsDto>();
        }

        /// <summary>
        /// 更新配置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="settings"></param>
        [SecurityDefine]
        [ApiDescriptionSettings(Name = "base")]
        public void UpdateSettings([ApiSeat(ApiSeats.ActionStart)] int id, [FromBody] UpdateSiteSettingsDto settings)
        {
            var input = settings.Adapt<SiteSetting>();
            // 从当前身份凭据中获取数据
            var tenantId = App.User.FindFirstValue(AppClaimConsts.TenantId);
            var hasTenant = Guid.TryParse(tenantId, out Guid tid);
            var history = _siteRepository.AsQueryable(false)
                 .Where(t => t.Id == id)
                 .Where(hasTenant, x => x.TenantId == tid)
                 .FirstOrDefault();

            if (history == null)
                throw Oops.Oh("更新站点配置失败,未能找到配置信息");

            var update = history.AsMergeObject(input, "Id", "CreatedTime", "UpdatedTime", "TenantId");
            update.UpdatedTime = DateTimeOffset.UtcNow; // 更新时间
            _siteRepository.Update(update);
        }

        /// <summary>
        /// 获取站点信息(匿名)
        /// </summary>
        /// <param name="tenantid">租户id</param>
        /// <returns></returns>
        [AllowAnonymous]
        public SiteInformationDto GetInformation([FromQuery] string tenantid)
        {
            var hasTenant = Guid.TryParse(tenantid, out Guid tid);
            return _siteRepository.AsQueryable(false)
                .Where(hasTenant, f => f.TenantId == tid)
                .FirstOrDefault()
                .Adapt<SiteInformationDto>();
        }

        /// <summary>
        /// 获取租户的站点配置信息
        /// </summary>
        /// <param name="tenantid"></param>
        /// <returns></returns>
        public SiteSetting GetTenantSettings(string tenantid = default)
        {
            var hasTenant = Guid.TryParse(tenantid, out Guid tid);
            return _siteRepository.AsQueryable(false)
                .Where(hasTenant, f => f.TenantId == tid)
                .FirstOrDefault();
        }
    }
}
